<?php

class index extends bhCommon {


    /**
     * 砍价
     */
    public function home()
    {
        $ad = array(
            'top' => $this->bargain->getAdvertisement(1, 5),
            'middle' => $this->bargain->getAdvertisement(2),
        );

        $sql = "SELECT A.id,A.price,A.bargain,A.time_end,B.cover_image,B.name FROM " . prefix('order') . " AS A INNER JOIN " . prefix('goods') . " AS B ON A.goods_id=B.id WHERE A.member_id={$this->member['id']} AND A.status=1 AND A.time_end>" . time() . ' ORDER BY A.id DESC';

        $bargain_goods = p_fetchall($sql);
        if ($bargain_goods) {
            foreach ($bargain_goods as & $value) {
                $value['cover_image'] = getImage($value['cover_image']);
                $value['surplus_time'] = $value['time_end'] - time();
                $value['surplus_money'] = round($value['price'] - $value['bargain'], 2);
                $value['d'] = $value['h'] = $value['m'] = $value['s'] = '00';
            }
        }

        $goods = p_getall('goods', array('is_delete' => 1, 'status' => 1, 'is_home' => 2), array(), array(), 'sort desc,id desc');
        $goods = arrayImage($goods, 'cover_image');

        $roll_content = p_getall('roll');
        if ($roll_content) {
            foreach ($roll_content as & $i) {
                $i['image'] = getImage($i['image']);
            }
        }

        $config = array(
            'integral_name' => getConfig('integral_name', '积分'),
            'xcx_title' => getConfig('xcx_title'),
            'video_id' => getConfig('video_id'),
            'screen_id' => getConfig('screen_id'),
            'coin_font_color' => getConfig('coin_font_color', '#d3870f'),
            'single_ad' => getConfig('single_ad'),
            'roll_content_bg' => getConfig('roll_content_bg', '#fff'),
            'roll_content' => $roll_content
        );



        $images = array(
            'bg' => $this->images('home_bg', 'bg.jpg'),
            'circle_img' => $this->images('circle_img', 'bg12.png'),
            'pop_gold' => $this->images('pop_gold', 'pop_gold.png'),
        );

        $data = array(
            'ad' => $ad,
            'bargain_goods' => $bargain_goods,
            'goods' => $goods,
            'config' => $config,
            'images' => $images,
            'member' => $this->member,
            'is_auth' => $this->is_auth
        );

        $this->json($data);
    }


    /**
     * 首页悬浮
     */
    public function suspension()
    {
        $sql = "SELECT * FROM " . prefix('task') . " WHERE `position` = 1 OR `position` = 3" . get_uniacid();
        $task = p_fetchall($sql);
        $new_task = array();
        if ($task) {
            $same_day = strtotime(date('Y-m-d'));
            foreach ($task as & $line) {
                $w = array(
                    'member_id' => $this->member['id'],
                    'task_id' => $line['id'],
                    'same_day' => $same_day
                );
                $m = p_get('task_member', $w);
                $line['state'] = 1;
                $line['is_succ'] = 1;
                $line['succ_num'] = 0;

                $line['num'] = 0;

                if (!empty($m)) {

                    $line['num'] = $m['number'];

                    if (in_array($line['type'], array(1,3))) {
                        $line['state'] = 3;
                        $line['succ_num'] = 1;
                    } else {
                        $line['succ_num'] = $m['number'];
                        $line['state'] = $m['number'] >= $line['number'] ? 3 : 1;
                    }
                } else {
                    if ($line['type'] == 1) {
                        $sql = "SELECT COUNT(*) FROM " . prefix('luck_participate') . " WHERE member_id={$this->member['id']} AND created >= {$same_day}";
                        $num = p_fetchcolumn($sql) ? : 0;

                        if ($line['number'] <= $num) {
                            $line['state'] = 2;
                            $line['num'] = $line['number'];
                        } else {
                            $line['num'] = $num;
                        }

                    }
                }

                if ($line['state'] != 3) {
                    $new_task[] = $line;
                    if (count($new_task) >= 6) {
                        break;
                    }
                }
            }
        }
        $data = array(
            'task' => $new_task,
            'member' => $this->member
        );
        $this->json($data);
    }


    /**
     * 砍价详情
     */
    public function bargain()
    {
        $id = intval($this->get('id'));

        $order = p_get('order', array('id' => $id));

        if (empty($order)) {
            $this->json('砍价不存在', 0);
        }
        $order['goods'] = $this->get('goods', array('id' => $order['goods_id']));
        $order['goods'] = getImage($order['goods']);

    }


    /**
     * 规则
     */
    public function rule()
    {
        $type = $this->get('type', 1);

        if ($type == 1) {
            $image = $this->images('hd_rule_img_top', 'hd_rule_img_top.png');
        } else {
            $image = $this->images('cj_rule_img_top', 'cj_rule_img_top.png');
        }


        $rule = p_getall('rule', array('position' => $type), array(), '', 'sort desc');

        if ($rule) {
            foreach ($rule as & $value) {
                $value['content'] = json_decode($value['content'], true);
            }
        }

        $data = array(
            'rule' => $rule,
            'image' => $image
        );
        $this->json($data);
    }


    /**
     * 互助群
     */
    public function group()
    {
        $type = $this->get('type', 0);
        if(empty($type)){
            $group_type = p_getall('group_type');
            $type = $group_type[0]['id']  ?? 0;
            $group = p_getall('group', array('status' => 1,"type"=>$type));
        }else{
            $group = p_getall('group', array('status' => 1,"type"=>$type));
        }
        $group = arrayImage($group, 'code');
        $group = arrayImage($group, 'bg');

        $group_type = p_getall('group_type');

        $data = array(
            'group' => $group,
            'group_type' => $group_type
        );
        $this->json($data);
    }

    /**
     * 互助群
     */
    public function get_group()
    {
        $integral = p_getcolumn("config", array('key' => 'get_group'), 'value');
        if (!$this->bargain->change_integral($this->member['id'], $integral, 11)) {
            $this->json(['code' => -999, "msg" => "积分不足{$integral},去获取积分吧"]);
            exit;
        }
        $this->json([]);
    }
}